/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ", "ajaxServ","formServ","sysmanMain","init","commonServ"], 
	function($,msgServ,ajaxServ,formServ,sysmanMain,init,commonServ) {

	'use strict';

	var moduleEl, formEl, private_method = 0,public_method=0, formData = 0, homeUrl = '';
	var pageDoc
	var public_method={
		
		domReady:function (doc) {
			$.messager.progress({				
				msg:'loading...',
				interval:1000
			});
			if (window.console) {
	        console.info('login-Sign in modules gets ready and  been loaded.');
			}
			
			
	        pageDoc = doc
			 $("a.btn").bind('click',public_method.loginclick);
			 
			 $(document).keypress(function(e) {  
			    // 回车键事件  
		       if(e.which == 13) {  
		           public_method.loginclick();  
		       }  
		   }); 
	   
		   var org_name = commonServ.getHostpital()
		   var _orgEl = $("div.navbar span.hospital-name")
		   $(_orgEl).text(org_name)
		   $('#loginname').focus();
		   $.messager.progress('close')
		 
    	},
			/**
		 * 
		 * 登录按钮点击handle
		 * 
		 * @param {Event}
		 *            典型 DOM event 对象
		 * @param {HTMLElement}
		 *            element The nearest HTML element with a data-type
		 *            attribute specified or null if there is none.
		 * @param {string}
		 *            elementType The value of data-type for the nearest element
		 *            with that attribute specified or null if there is none.
		 * @returns {void}*
		 */
		loginclick:function(event) {			

			if(event){
				event.preventDefault();
				event.stopPropagation();
			}
			formEl = $('form');
			
			formData = formServ.getDataObj({
				form : formEl
			});

			var rules, messages, validateOptions;

			rules = {
				username : "required"
			};
			messages = {
				username : '用户名不能为空'
			};
			validateOptions = {
				debug : true,
				targetForm : formEl,
				rules : rules,
				messages : messages,
				submitHandler : private_method.loginHandler
			};
			
			$.messager.progress({
				title:'请稍后',
				msg:'正在登录系统...',
				interval:1000
			});
			formServ.onSubmit(validateOptions);

				// isValid = formServ.validate(validateOptions);

				// context.broadcast('todostatuschange');
			return false;
		}
	}
	
	
    
	var private_method = {
		loginHandler : function(event) {
			
			var _opt = {
				url : '/biz/login.action',
				data : formData,
				callback : private_method.loginOK,
				contentType : 'form'
			};

			ajaxServ.runAjax(_opt);

		},
		logout : function() {

				

		},
		loginOK : function(data, ressionStr, statusObj) {
			$.messager.progress('close')
			if (data && data.retStatus) {
				if(data.retStatus==='success') {
										
					private_method.processPageDirect(data);						
				}else{
				
					msgServ.Msgslide("用户名或密码错误");
						

				}

			}
			// alert('login ok !!!');
		},
		/**
		 * 登录成功，处理页面跳转
		 * @param _options.retStr	首页地址
		 * @param _options.retData	登录用户
		 */
		processPageDirect:function(_options){
			if(window.console)
				console.info('process login ...')
			switch(_options.retStatus){
				case 'success':
					sysmanMain.setCurrentUser(_options.retData);
					init.setCurrentUser(_options.retData);
					window.location.href = _options.retStr;
					break;
				default:
					var patharr = pageDoc.location.pathname.split('/')
       				var rootPath = patharr[1]
					var url = '/' +rootPath
					top.window.location.href=url
					break;
			}
			
		}

	};
	
		

		
	
		
	return public_method;

	

});